package com.chen.controller;

import com.chen.anno.Log;
import com.chen.pojo.Emp;
import com.chen.pojo.EmpQueryParam;
import com.chen.pojo.PageResult;
import com.chen.pojo.Result;
import com.chen.service.EmpService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDate;
import java.util.Arrays;
import java.util.List;

/*
员工管理Controller
 */
@Slf4j
@RestController
@RequestMapping("/emps")
public class EmpController {
    @Autowired
    public EmpService empService;
//    /*
//    分页查询员工信息
//     */
//    @GetMapping()
//    public Result page(@RequestParam(defaultValue = "1") Integer page,
//                       @RequestParam(defaultValue = "10")Integer pageSize,
//                       String name,Integer gender,
//                       @DateTimeFormat(pattern = "yyyy-MM-dd") LocalDate begin,
//                       @DateTimeFormat(pattern = "yyyy-MM-dd") LocalDate end){
//        log.info("分页查询{},{},{},{},{},{}",page,pageSize,name,gender,begin,end);
//        PageResult<Emp> pageResult =empService.page(page,pageSize,name,gender,begin,end);
//        return Result.success(pageResult);
//
//    }
     /*
    分页查询员工信息
     */
@GetMapping()
public Result page(EmpQueryParam empQueryParam) {
    log.info("分页查询{}",empQueryParam);
    PageResult<Emp> pageResult =empService.page(empQueryParam);
    return Result.success(pageResult);
}
@Log
@PostMapping
public Result add(@RequestBody Emp emp){
    log.info("添加员工:+{}",emp);
    empService.add(emp);
    return Result.success();
}
/*
数组接受请求参数
 */
//@DeleteMapping
//public Result dalete(Integer[] ids){
//    log.info("删除员工:+{}", Arrays.toString(ids));
//    return Result.success();
//}
/*
批量删除员工
 */
@Log
@DeleteMapping
//使用List接受请求参数 RequestParam注解不能省略
public Result delete(@RequestParam  List<Integer> ids){
    log.info("删除员工:+{}", ids);
    empService.delete(ids);
    return Result.success();
}
/*
查询员工信息
 */
@GetMapping("/{id}")
public Result getInfo(@PathVariable Integer id) {
    log.info("查询员工信息+{}",id);
    Emp empInfo =empService.getInfo(id);
    return Result.success(empInfo);
}
/*
修改员工信息
 */
@Log
@PutMapping
public Result updateEmpInfo(@RequestBody Emp emp) {
    log.info("修改员工信息:+{}",emp);
    empService.updateInfo(emp);
    return Result.success();
}
@GetMapping("/list")
    public Result list() {
    log.info("查询全部员工信息");
    List<Emp> empList =empService.list();
    return Result.success(empList);
}
}
